package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(tags = "登录接口")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtEncoder jwtEncoder;


    //@CrossOrigin(origins = "http://localhost:3000")
    @ApiOperation(value = "管理员登录")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin(AdminLoginParam param) throws BusinessException {
        log.debug("管理员登录");
        AdminVO adminVO = adminService.login(param);

        LoginVO loginVO = new LoginVO();
        loginVO.setId(adminVO.getId());
        loginVO.setNickname(adminVO.getNickname());
        loginVO.setJwt(jwtEncoder.generateToken(loginVO));
        return new Result<>(loginVO);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/passport/user/login")
    public Result<LoginVO> userLogin(UserLoginParam param)
        throws BusinessException{
        log.debug("用户登录参数:{}", param);
        LoginVO loginVO = loginService.login(param);
        return new Result<>(loginVO);
    }
}
